package com.powergisol.gis.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.powergisol.gis.dto.ExcelRectangularPillarModelDTO;
import com.powergisol.gis.entity.TabCoordinateSystem;
import com.powergisol.gis.util.XyChangeUtil;
import com.powergisol.gis.vo.ExcelInFailedDataVo;
import com.powergisol.gis.vo.TabRectangularPillarVO;
import com.powergisol.gis.vo.TaskResultVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

import java.math.BigDecimal;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author : laishihao
 * @date : 2020/11/9
 * description :
 */
public class RectangularPillarModelListener extends AnalysisEventListener {

    private final static Pattern NUMBER_PATTERN = Pattern.compile("([+\\-]?[0-9]+[.]?[\\d]*)");

    private TabRectangularPillarVO vo;

    //坐标系bean
    TabCoordinateSystem system;

    CacheManager cacheManager;

//    String excelName;
    //字段值所在列数
    private Integer valueColumn;

    private ExcelRectangularPillarModelDTO rectangularPillarModelDTO;
    private BigDecimal lon;
    private BigDecimal lat;
    private BigDecimal altitude;


    public RectangularPillarModelListener() {

    }

    public RectangularPillarModelListener(TabRectangularPillarVO vo, TabCoordinateSystem system,
                                          CacheManager cacheManager, BigDecimal lon, BigDecimal lat, BigDecimal altitude,
                                          ExcelRectangularPillarModelDTO rectangularPillarModelDTO) {
        this.vo = vo;
        this.system = system;
        this.cacheManager = cacheManager;
        this.lon = lon;
        this.lat = lat;
        this.altitude = altitude;
        this.rectangularPillarModelDTO = rectangularPillarModelDTO;
    }


    @Override
    public synchronized void invoke(Object o, AnalysisContext context) {
        ArrayList data = (ArrayList) o;
        try {
            if (data.isEmpty()) {
                return;
            }
            int j = 0;
            int dataSize = data.size();
            for (int i = 0; i < dataSize; i++) {
                if (j == 1) {
                    return;
                }
                String str = String.valueOf(data.get(i));
                if (StringUtils.isBlank(str)) continue;

                //如果列数存在
                if (null == valueColumn && str.equals("字段值")) {
                    valueColumn = i;
                }

                switch (str) {
                    case "Hcdm01":
                        vo.setHcdm01(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "Hcdm02":
                        vo.setHcdm02(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "Hcdm03":
                        vo.setHcdm03(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "Hcdm04":
                        vo.setHcdm04(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : Integer.parseInt(String.valueOf(data.get(valueColumn))));
                        j++;
                        break;
                    case "Hcdm05":
                        vo.setHcdm05(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "Hcdm06":
                        vo.setHcdm06(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : String.valueOf(data.get(valueColumn)));
                        j++;
                        break;
                    case "KZ021":
                        if (NUMBER_PATTERN.matcher(String.valueOf(data.get(valueColumn))).matches()) {
                            vo.setKZ021(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : BigDecimal.valueOf(Double.parseDouble(String.valueOf(data.get(valueColumn)))));
                        }
                        j++;
                        break;

                    case "KZ022":
                        if (NUMBER_PATTERN.matcher(String.valueOf(data.get(valueColumn))).matches()) {
                            vo.setKZ022(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : BigDecimal.valueOf(Double.parseDouble(String.valueOf(data.get(valueColumn)))));
                        }
                        j++;
                        break;
                    case "KZ023":
                        vo.setKZ023(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : BigDecimal.valueOf(Double.parseDouble(String.valueOf(data.get(valueColumn)))));
                        j++;
                        break;
                    case "KZ024":
                        vo.setKZ024(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : BigDecimal.valueOf(Double.parseDouble(String.valueOf(data.get(valueColumn)))));
                        j++;
                        break;
                    case "KZ025":
                        vo.setKZ025(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : BigDecimal.valueOf(Double.parseDouble(String.valueOf(data.get(valueColumn)))));
                        j++;
                        break;
                    case "KZ026":
                        vo.setKZ026(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : BigDecimal.valueOf(Double.parseDouble(String.valueOf(data.get(valueColumn)))));
                        j++;
                        break;
                    case "KZ027":
                        vo.setKZ027(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : BigDecimal.valueOf(Double.parseDouble(String.valueOf(data.get(valueColumn)))));
                        j++;
                        break;
                    case "KZ091":
                        vo.setKZ091(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : Integer.parseInt(String.valueOf(data.get(valueColumn))));
                        j++;
                        break;
                    case "KZ092":
                        vo.setKZ092(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : Integer.parseInt(String.valueOf(data.get(valueColumn))));
                        j++;
                        break;
                    case "KZ093":
                        vo.setKZ093(valueColumn >= dataSize ? null : null == data.get(valueColumn) ? null : Integer.parseInt(String.valueOf(data.get(valueColumn))));
                        j++;
                        break;
                    default:
                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
//            return;
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveDate(vo);
    }

    private void saveDate(TabRectangularPillarVO vo) {

        if (null ==vo.getKZ021() || !NUMBER_PATTERN.matcher(String.valueOf(vo.getKZ021())).matches()) {
            dealFail("坐标Xl需为数字");
            return;
        }
        if (null ==vo.getKZ022() || !NUMBER_PATTERN.matcher(String.valueOf(vo.getKZ022())).matches()) {
            dealFail("坐标Yw需为数字");
            return;
        }

        //储存原始值
        vo.setOKZ021(vo.getKZ021());
        vo.setOKZ022(vo.getKZ022());
        vo.setOKZ023(vo.getKZ023());
        vo.setOKZ024(vo.getKZ024());
        vo.setOKZ025(vo.getKZ025());
        vo.setOKZ026(vo.getKZ026());
        vo.setOKZ027(vo.getKZ027());



        //转换单位 (mm 转 m )
        BigDecimal KZ021 = lon.multiply(new BigDecimal(1000)).add(vo.getKZ021()).divide(new BigDecimal(1000));
        BigDecimal KZ022 = lat.multiply(new BigDecimal(1000)).add(vo.getKZ022()).divide(new BigDecimal(1000));
        BigDecimal KZ023 =  altitude.multiply(new BigDecimal(1000)).add(vo.getKZ023()).divide(new BigDecimal(1000));
        vo.setKZ023(KZ023);
        vo.setKZ021(KZ021);
        vo.setKZ022(KZ022);

        if(vo.getOKZ024()!=null && vo.getOKZ024().toString().equals("0.0")){
            vo.setKZ025(mmTom(vo.getOKZ025()));
            vo.setKZ026(mmTom(vo.getOKZ026()));
        }else {
            vo.setKZ025(mmTom(vo.getOKZ026()));
            vo.setKZ026(mmTom(vo.getOKZ025()));
        }

        vo.setKZ027(mmTom(vo.getKZ027()));
        rectangularPillarModelDTO.getTabRectangularPillarsList().add(vo);
    }

    //转换单位 (mm 转 m （毫米 转 米）)
    private BigDecimal mmTom(BigDecimal mm) {
        if (mm != null) {
            return mm.divide(new BigDecimal(1000), 10, BigDecimal.ROUND_HALF_UP);
        }
        return null;
    }

    private void dealFail(String msg) {
        List<ExcelInFailedDataVo> listFailed = new ArrayList<>();
        ExcelInFailedDataVo failedDataVo = new ExcelInFailedDataVo();
        failedDataVo.setReason(msg);
        failedDataVo.setRowIndex(vo.getExcelName()+"中sheet "+vo.getSheetName());
        listFailed.add(failedDataVo);
        Cache cache = cacheManager.getCache("cache-data");
        TaskResultVo resultVo = new TaskResultVo();
        resultVo.setListFailed(listFailed);
        cache.put(vo.getSheetName()+vo.getUuid() + "sb", resultVo);
    }


}
